export const colorList = [
  ['#DFEDD5', '#B7D1A5'],
  ['#DDE8BE', '#BFCC9D'],
  ['#F2CDBB', '#E3A98D'],
  ['#EBD9B7', '#DEBD81'],
  ['#CAE7B9', '#A5DB86'],
  ['#AAE3E2', '#7ED6D5'],
  ['#C5D5EA', '#92AFD6'],
  ['#B6EFD4', '#77D1A7'],
  ['#D3D0CB', '#C2B8A7'],
  ['#AEE8D5', '#85C9B3']
]

export const defaultModeSource1 = {
  '舒缓':{
    icon:'/static/svg/mode/type11.svg',
    bgColor:'#F2E6B3',
    opColor:'#D9B525',
  },
  '标准':{
    icon:'/static/svg/mode/type12.svg',
    bgColor:'#FCC9C7',
    opColor:'#DEA5A0',
  },
  '强劲':{
    icon:'/static/svg/mode/type13.svg',
    bgColor:'#D4DAFB',
    opColor:'#8695EB',
  }
}
export const defaultModeSource2 = {
  '快乐享受':{
    icon:'/static/svg/mode/type21.svg',
    bgColor:'#BDF2E7',
    opColor:'#88C2B6',
  },
  '高低起伏':{
    icon:'/static/svg/mode/type22.svg',
    bgColor:'#F5DED7',
    opColor:'#D6B4A9',
  },
  '旋转木马':{
    icon:'/static/svg/mode/type23.svg',
    bgColor:'#C8E1FA',
    opColor:'#99B3CC',
  }
}

export async function getDefaultMode(type){
  let defaultMode = uni.getStorageSync(`defaultMode${type}`)
  //if(!defaultMode){
    let {data:res} = await fetchDefaultMode()
    uni.setStorageSync('defaultConfig', res.choushui)
    uni.setStorageSync('defaultMode1', res.yiliao_solutions.map(x=>{
      return {
        ...x,
        ...defaultModeSource1[x.name]
      }
    }))
    uni.setStorageSync('defaultMode2', res.yiqu_solutions.map(x=>{
      return {
        ...x,
        ...defaultModeSource2[x.name]
      }
    }))
  //}
  return uni.getStorageSync(`defaultMode${type}`)
}
//创建自定义模式 type 1 医疗 2 趣疗
export function createMode(type, option) {
  // let option = {
  //   name: '自定义',
  //   items: [{
  //       "choushui_sec": 0.5,
  //       "strength": 2,
  //       "sleep_sec": 0.1
  //     },
  //     {
  //       "choushui_sec": 0.1,
  //       "strength": 3,
  //       "sleep_sec": 0.1
  //     },
  //     {
  //       "choushui_sec": 0.1,
  //       "strength": 2,
  //       "sleep_sec": 0.1
  //     },
  //     {
  //       "choushui_sec": 0.1,
  //       "strength": 3,
  //       "sleep_sec": 0.1
  //     }
  //   ]
  // }
  let success = isRepeat(type,option.name)
  if(!success){
    return {
      success,
      msg:'模式名称重复'
    }
  }
  let selfModeList = uni.getStorageSync(`selfModeList${type}`) || []
  let modeColor = getRoundColor(type)
  selfModeList.push({
    ...option,
    bgColor:modeColor[0],
    opColor:modeColor[1]
  })
  uni.setStorageSync(`selfModeList${type}`, selfModeList)

  return {
    success:true
  }
}

//更改名称
export function editModeName(type,oldName,newName){
  let success = isRepeat(type, newName)
  if(!success){
    return {
      success:false,
      msg:'模式名称重复'
    }
  }

  let selfModeList = uni.getStorageSync(`selfModeList${type}`) || []
  selfModeList.some((x,index)=>{
    if(x.name == oldName){
      selfModeList[index].name = newName
      return true
    }
  })
  uni.setStorageSync(`selfModeList${type}`, selfModeList)
  console.log(selfModeList,type,oldName,newName)
  return {
    success:true
  }
}
//保存单项配置
export function saveMode(type,option){
  console.log('保存模式',type,option)
  let selfModeList = uni.getStorageSync(`selfModeList${type}`) || []
  selfModeList.some((x,index)=>{
    if(x.name == option.name){
      selfModeList[index] = option
      return true
    }
  })
  uni.setStorageSync(`selfModeList${type}`, selfModeList)
  return {
    success:true
  }
}
//获取单项配置
export function getMode(type,name){
  let selfModeList = uni.getStorageSync(`selfModeList${type}`) || []
  //console.log(selfModeList)
  let mode
  selfModeList.some((x,index)=>{
    if(x.name == name){
      mode = x
      return true
    }
  })
  return {
    success:true,
    data:mode
  }
}
//删除配置
export function delMode(type,name){
  let selfModeList = uni.getStorageSync(`selfModeList${type}`) || []
  selfModeList.some((x,index)=>{
    if(x.name == name){
      selfModeList.splice(index,1)
      return true
    }
  })
  uni.setStorageSync(`selfModeList${type}`, selfModeList)
  return {
    success:true
  }
}

//检测模式名称是否重复
function isRepeat(type, name) {
  let selfModeList = uni.getStorageSync(`selfModeList${type}`) || []

  if (selfModeList.filter(x => {
      return x.name == name
    }).length !== 0) {
    return false
  }
  return true
}

//获取背景和文字颜色
function getRoundColor(type){
  let selfModeList = uni.getStorageSync(`selfModeList${type}`) || []
  let isUsedColor = selfModeList.map(x=>{
    return x.bgColor
  })
  let canUseColor = colorList.filter(x=>{
    return !isUsedColor.includes(x[0])
  })
  return canUseColor[0]
}

//通过网络获取默认模式
export function fetchDefaultMode(){
  return new Promise((resolve,reject)=>{
    uni.request({
      url:'https://www.machineeye.cn/api/getJieyaoConfig',
      method:'POST',
      success(res){
        resolve(res.data)
      }
    })
  })
 
}